package com.zmc.admin.controller.system;

import com.zmc.admin.infrastructure.bean.vo.post.SysPostVo;
import com.zmc.admin.infrastructure.constant.ErrorCodeConstants;
import com.zmc.admin.infrastructure.entity.SysPostEntity;
import com.zmc.admin.infrastructure.bean.query.SysPostQuery;
import com.zmc.admin.infrastructure.service.SysPostService;

import com.zmc.common.annotation.PreventRepeatSubmit;
import com.zmc.common.enums.RequestSourceEnum;
import com.zmc.common.exception.base.WebDisplayException;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.common.utils.token.AuthTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 岗位信息操作处理
 * @author zmc
 */
@RestController
@RequestMapping("/post")
public class SysPostController {
    @Autowired
    private SysPostService postService;

    @Autowired
    private AuthTokenService authTokenService;

    /**
     * 获取岗位列表
     */
    @PostMapping("/list")
    public R list(@RequestBody SysPostQuery query) {
        PageUtils page = postService.pagePostList(query);
        return R.page(page);
    }

    @PostMapping("/export")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public void export(HttpServletResponse response, SysPostQuery query) {
        List<SysPostEntity> list = postService.selectPostList(query);
        ExcelUtil<SysPostEntity> util = new ExcelUtil<>(SysPostEntity.class);
        util.exportExcel(response, list, "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PostMapping(value = "/info")
    public R getInfo(@RequestBody SysPostVo vo) {
        return R.ok(postService.getById(vo.getPostId()));
    }

    /**
     * 新增岗位
     */
    @PostMapping("/add")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R add(@RequestBody SysPostEntity post) {
        if (!postService.checkPostNameUnique(post)) {
            throw new WebDisplayException(ErrorCodeConstants.POST_NAME_EXIT_EXCEPTION);
        } else if (!postService.checkPostCodeUnique(post)) {
            throw new WebDisplayException(ErrorCodeConstants.POST_CODE_EXIT_EXCEPTION);
        }
        post.setCreateBy(authTokenService.getUserInfo().getUsername());
        postService.save(post);
        return R.ok();
    }

    /**
     * 修改岗位
     */
    @PostMapping("/update")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R edit(@RequestBody SysPostEntity post) {
        if (!postService.checkPostNameUnique(post)) {
            throw new WebDisplayException(ErrorCodeConstants.POST_NAME_EXIT_EXCEPTION);
        } else if (!postService.checkPostCodeUnique(post)) {
            throw new WebDisplayException(ErrorCodeConstants.POST_CODE_EXIT_EXCEPTION);
        }
        post.setUpdateBy(authTokenService.getUserInfo().getUsername());
        postService.updateById(post);
        return R.ok();
    }

    /**
     * 删除岗位
     */
    @PostMapping("/delete")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R remove(@RequestBody SysPostVo vo) {
        postService.removeById(vo.getPostId());
        return R.ok();
    }

    /**
     * 获取岗位选择框列表
     */
    @PostMapping("/optionselect")
    public R optionSelect() {
        List<SysPostEntity> posts = postService.list();
        return R.ok(posts);
    }
}
